﻿using System;
using System.Runtime.InteropServices;

namespace AsTekMobile
{
    public class Sip
    {
        [DllImport("coredll.dll")]
        private static extern int SipShowIM(SIPStatus i);
        [DllImport("coredll.dll")]
        private static extern bool SipSetInfo(ref SIPINFO pSipInfo);
        [DllImport("coredll.dll")]
        private static extern bool SipGetInfo(ref SIPINFO pSipInfo);

        private static bool pvtVisible = false;
        private enum SIPStatus
        {
            SIPF_OFF = 0,
            SIPF_ON
        }
        private struct SIPINFO
        {
            public Int32 cbSize;
            public Int32 fdwFlags;
            public RECT rcVisibleDesktop;
            public RECT rcSipRect;
            public Int32 dwImDataSize;
            public Int32 pvImData;
        }
        private struct RECT
        {
            public Int32 left;
            public Int32 top;
            public Int32 right;
            public Int32 bottom;
        }


        // Mostrar el SIP
        public static void Show()
        {
            SipShowIM(SIPStatus.SIPF_ON);
        }

        // Ocultar el SIP
        public static void Hide()
        {
            SipShowIM(SIPStatus.SIPF_OFF);
        }

        public void SetPosition(Int32 top)
        {
            SIPINFO mySi = new SIPINFO();
            bool result = true;
            mySi.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(SIPINFO));
            result = SipGetInfo(ref mySi);
            mySi.rcSipRect.top = top;
            mySi.rcSipRect.bottom = top + 80;
            result = SipSetInfo(ref mySi);
        }

        public static bool Visible
        {
            get
            {
                return pvtVisible;
            }
            set
            {
                if (value == true)
                {
                    SipShowIM(SIPStatus.SIPF_ON);
                }
                else
                {
                    SipShowIM(SIPStatus.SIPF_OFF);
                }
                pvtVisible = value;
            }
        }
    }
}
